package com.wty.good.controller;

/**
 * @Author:wangtianyu
 * @Date：Created on 2020/4/23 0023 13:01
 */

import com.wty.commons.result.Result;
import com.wty.good.dto.GoodInfoDtoWty;
import com.wty.good.model.DataGridWty;
import com.wty.good.model.GoodInfoWty;
import com.wty.good.pojo.GoodWty;
import com.wty.good.service.GoodServiceWty;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 商品
 */
@RestController
@RequestMapping("goods")
public class GoodControllerWty {

    @Autowired
    private GoodServiceWty goodService;

    @ApiOperation(value = "添加商品基本信息")
    @ApiImplicitParam(name = "good",required = true,dataType = "GoodWty",dataTypeClass = GoodWty.class)
    @PostMapping("addGood")
    public Result<GoodWty> addGood(@RequestBody GoodWty good)throws Exception{
        good = goodService.addGood(good);
        return Result.OK(good);
    }

    @ApiOperation(value = "添加商品基本信息，图片，明细")
    @ApiImplicitParam(name = "info",required = true,dataType = "GoodInfoDtoWty",dataTypeClass = GoodInfoDtoWty.class)
    @PostMapping("addGoodInfo")
    public Result<GoodInfoWty> addInfo(@RequestBody GoodInfoDtoWty info)throws Exception{
        GoodInfoWty goodInfo = goodService.addGoodInfo(info);
        return Result.OK(goodInfo);
    }

    @ApiOperation(value = "分页查询商品明细信息")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "good", required = true, dataType = "GoodWty", dataTypeClass = GoodWty.class),
        @ApiImplicitParam(name = "page", required = true, dataType = "Long", dataTypeClass = Long.class),
        @ApiImplicitParam(name = "count", required = true, dataType = "Long", dataTypeClass = Long.class)
    })
    @PostMapping("selectAllListByPage/{page}/{count}")
    public Result<List<GoodInfoWty>> selectListByPage(@PathVariable Long page, @PathVariable Long count,@RequestBody GoodWty good) throws Exception {
        List<GoodInfoWty> goodInfos = goodService.selectListByPage(good.getGoodtitle(),page, count);
        return Result.OK(goodInfos);
    }

    @ApiOperation(value = "分页查询商品基本信息")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "page", required = true, dataType = "Long", dataTypeClass = Long.class),
        @ApiImplicitParam(name = "count", required = true, dataType = "Long", dataTypeClass = Long.class),
        @ApiImplicitParam(name = "good", required = true, dataType = "GoodWty", dataTypeClass = GoodWty.class)
    })
    @PostMapping("selectGoodList/{page}/{count}")
    public Result<DataGridWty<GoodWty>> selectGoodList(@PathVariable Long page, @PathVariable Long count, @RequestBody GoodWty good) throws Exception {
        DataGridWty<GoodWty> goods = goodService.selectGoodList(good.getGoodtitle(),page,count);
        return Result.OK(goods);
    }

    @ApiOperation(value = "根据商品ID查询商品明细")
    @ApiImplicitParam(name = "goodid",required = true,dataType = "Long",dataTypeClass = Long.class)
    @GetMapping("selectGoodInfoById/{goodid}")
    public Result<GoodInfoWty> selectGoodInfoById(@PathVariable Long goodid)throws Exception{
        GoodInfoWty goodInfo = goodService.selectGoodInfoById(goodid);
        return Result.OK(goodInfo);
    }

    @ApiOperation(value = "批量上架")
    @ApiImplicitParam(name = "ids",required = true,dataType = "String",dataTypeClass = String.class)
    @PutMapping("upGoods/{ids}")
    public Result<Integer> upGoods(@PathVariable String ids)throws Exception{
        List<Long> goodids = new ArrayList<>();
        String[] split = ids.split(",");
        for (int i = 0; i < split.length; i++) {
            goodids.add(Long.parseLong(split[i]));
        }
        Integer count = goodService.upGoods(goodids);
        return Result.OK(count);
    }

}
